import FYBatchParent from "./FYBatchParent";


export default class FYBatchNodeInfo {
    /** 索引 */
    private index: number = 0;
    /** 原始坐标 */
    private srcPos: cc.Vec2 = new cc.Vec2(0, 0);
    /** 当前node对象 */
    private node: cc.Node = null;
    /** 原始父对象 */
    private srcParent: cc.Node = null;
    /** 新父对象 */
    private newParent: cc.Node = null;
    /** 是否需要同步坐标 */
    private needSyncPos = false;
    /** 是否需要同步透明度变化 */
    private needSyncOpacity: boolean = false;
    /** 是否需要同步角度变化 */
    private needSyncRotation: boolean = false;
    /** 销毁回调 */
    private cbDestroy = null;
    /** 批处理的父对象 */
    private batchParent: FYBatchParent = null;


    public init(index: number, node: cc.Node, srcParent: cc.Node, newParent: cc.Node, cbDestroy: Function, needSyncPos = false, needSyncOpacity = false, needSyncRotation = false) {
        if (!cc.isValid(node) || cc.isValid(srcParent) || cc.isValid(newParent)) {
            return;
        }

        this.index = index;
        this.node = node;
        this.srcParent = srcParent;
        this.newParent = newParent;
        this.needSyncPos = needSyncPos;
        this.needSyncOpacity = needSyncOpacity;
        this.needSyncRotation = needSyncRotation;
        this.srcPos = this.node.position.clone();
        this.node.setParent(this.newParent);

        this.cbDestroy = cbDestroy;

        let batchParent = this.srcParent.getComponent(FYBatchParent)
        if (!batchParent) {
            batchParent = this.srcParent.addComponent(FYBatchParent);
        }

        batchParent.init(this.onActiveChange.bind(this), this.needSyncPos ? this.onPositionChange.bind(this) : null, this.needSyncRotation ? this.onRotationChange.bind(this) : null, this.onDestroy.bind(this));
        this.batchParent = batchParent;
    }

    public removeMsg() {
        if (this.batchParent) {
            this.batchParent.removeMsg();
            if (this.node && this.node.isValid && this.srcParent && this.srcParent.isValid) {
                this.node.setParent(this.srcParent);
                this.node.setPosition(this.srcPos);
            }
        }
    }

    /**
     * 同步坐标
     */
    public syncPos() {
        if (this.srcParent === null || this.newParent === null || this.node === null) {
            return;
        }
        // 获取世界坐标
        let worldPos = this.srcParent.convertToWorldSpaceAR(this.srcPos);
        // 获取在新父对象的本地坐标
        let localPos = this.newParent.convertToNodeSpaceAR(worldPos);

        this.node.setPosition(localPos);

        if (this.needSyncOpacity) {
            this.node.opacity = this.srcParent.opacity;
        }
    }

    onPositionChange() {
        this.syncPos();
    }

    onActiveChange(bActive: boolean) {
        if (this.node === null) {
            return;
        }
        this.node.active = bActive;
    }

    onRotationChange() {
        if (this.node === null) {
            return;
        }
        // this.node.rotation = this.srcParent.rotation;
        this.node.angle = this.srcParent.angle;
    }

    onDestroy() {
        if (this.node) {
            this.node.destroy();
        }

        this.node = null;
        this.srcParent = null;
        this.newParent = null;

        if (this.cbDestroy) {
            this.cbDestroy(this.index);
        }
    }
}
